# -*- coding: utf-8 -*-
from django.conf import settings

from common.utils.types import Enum
from common.utils.orm import ArmoryOrm
import datetime

orm = ArmoryOrm()
orm.init_conf(settings.ADMIN_CONF)

FORBIDDEN_ROLE = 0
USER_NOT_DELETED = 0
USER_DELETED = 1
USER_DISABLE = 0
USER_ENABLE = 1

ROLE = Enum({
    "FORBIDDEN": (0, "forbidden"),
    "OUT_GENERAL_AGENT": (1, "OUT_GENERAL_AGENT"),
    "OUR_FINANCE": (2, "OUR_FINANCE"),
    "GENERAL_AGENT": (3, "GENERAL_AGENT"),
    "FINANCE": (4, "FINANCE"),
    "ADMIN": (5, "admin")
})

PERMISSION = Enum({
    "NONE": (0, "none"),
    "READ": (1, "read"),
    "WRITE": (2, "write"),
})


PERMISSION_MOD = Enum({
    "Operation Management": (1L, "运行管理"),
    "Cards Management": (2L, "卡片管理"),
    "Range Management": (3L, "范围管理"),
    "Order Management": (4L, "订单管理"),
    "Bank Record Management": (5L, "银行记录管理"),
    "Console Management": (6L, "控制台管理"),
    "Exe Management": (7L, "Exe管理")
})


ACTION = {
    'POST': 1,
    'PUT': 2,
    'PATCH': 2,
    'DELETE': 3,
    'GET': 4
}

RESOURCE = {
    '^user/login/?$': 'login',
    '^mch/(?P<mch_id>\d+)/?$': 'single_mch',
}


class User(orm.Model):
    __tablename__ = 'user'
    id = orm.Column(orm.BigInteger, primary_key=True)
    nickname = orm.Column(orm.VARCHAR)
    password = orm.Column(orm.VARCHAR)
    role = orm.Column(orm.SmallInteger)
    perm = orm.Column(orm.VARCHAR, default=0)
    created_at = orm.Column(orm.DATETIME)
    updated_at = orm.Column(orm.DATETIME)
    mch_ids = orm.Column(orm.VARCHAR)


class Permission(orm.Model):
    __tablename__ = 'permission'
    id = orm.Column(orm.BigInteger, primary_key=True)
    url = orm.Column(orm.VARCHAR)
    action = orm.Column(orm.VARCHAR)
    permission = orm.Column(orm.SmallInteger)
    permission_mod = orm.Column(orm.VARCHAR)
    min_role = orm.Column(orm.SmallInteger)
    user_perm = orm.Column(orm.VARCHAR) # 需要满足的用户权限
    desc = orm.Column(orm.VARCHAR)
    created_at = orm.Column(orm.DATETIME)
    updated_at = orm.Column(orm.DATETIME)


class UserToken(orm.Model):
    __tablename__ = 'user_token'
    user_id = orm.Column(orm.BigInteger, primary_key=True)
    token = orm.Column(orm.VARCHAR, primary_key=True)
    deleted = orm.Column(orm.SmallInteger)
    created_at = orm.Column(orm.DATETIME,default=datetime.datetime.now())
    updated_at = orm.Column(orm.DATETIME, default=datetime.datetime.now())


class Record(orm.Model):
    __tablename__ = 'record'
    id = orm.Column(orm.BigInteger, primary_key=True)
    resource = orm.Column(orm.VARCHAR)
    resource_id = orm.Column(orm.VARCHAR)
    action = orm.Column(orm.SmallInteger)
    content = orm.Column(orm.Text)
    ip = orm.Column(orm.VARCHAR)
    city = orm.Column(orm.VARCHAR, default='')
    operator = orm.Column(orm.BigInteger, default=0)
    created_at = orm.Column(orm.DATETIME)


class Role(orm.Model):
    __tablename__ = 'role'
    id = orm.Column(orm.BigInteger, primary_key=True)
    rolename = orm.Column(orm.VARCHAR)
    permissions = orm.Column(orm.VARCHAR)
    mch_ids = orm.Column(orm.VARCHAR)
    enabled = orm.Column(orm.SmallInteger)
    deleted = orm.Column(orm.SmallInteger)
    created_at = orm.Column(orm.DATETIME)
    updated_at = orm.Column(orm.DATETIME)
